<template>
  <div class="cases-container">
    <h1 class="title">用户案例</h1>
    <p class="subtitle">图片对比效果</p>
    
    <!-- 图片对比部分 -->
    <div class="comparison-grid">
      <div class="comparison-item" v-for="(item, index) in cases" :key="index">
        <div class="comparison-wrapper">
          <!-- 底层图片（吉卜力风格） -->
          <img :src="item.after" alt="吉卜力风格" class="image-base" />
          
          <!-- 顶层图片（原图）带透明度控制 -->
          <div class="image-overlay" :style="{ clipPath: `inset(0 0 0 ${item.sliderPosition}%)` }">
            <img :src="item.before" alt="原图" class="image-top" />
          </div>
          

          <!-- 垂直滑块 -->
          <div class="slider-vertical" 
               @mousedown="(e) => startSliding(e, index)"
               @touchstart="(e) => startSliding(e, index)"
               :style="{ left: `${item.sliderPosition}%` }">
            <div class="slider-line"></div>
            <div class="slider-button"></div>
          </div>
        </div>
        <div class="comparison-label">
          <span>原图</span>
          <span>吉卜力风格</span>
        </div>
      </div>
    </div>
    
    <!-- 文生图案例部分 -->
    <h2 class="section-title">文生图案例</h2>
    <p class="section-subtitle">通过文字描述生成吉卜力风格图片</p>
    
    <div class="masonry-container">
      <div 
        v-for="(item, index) in textToImageCases" 
        :key="index"
        class="masonry-item"
      >
        <div class="masonry-content">
          <img :src="item.image" :alt="item.prompt" class="masonry-image" />
          <div class="masonry-caption">
            <p class="prompt-text">{{ item.prompt }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onBeforeUnmount } from 'vue'

interface ComparisonCase {
  before: string
  after: string
  sliderPosition: number
}

interface TextToImageCase {
  prompt: string
  image: string
}

const cases = ref<ComparisonCase[]>([
  {
    before: '/demo/image.png',
    after: '/demo/image_after.png',
    sliderPosition: 50
  },
  {
    before: '/demo/photo2.jpg',
    after: '/demo/photo2.jpg',
    sliderPosition: 50
  },
  {
    before: '/demo/photo3.jpg',
    after: '/demo/photo3.jpg',
    sliderPosition: 50
  },
  {
    before: '/demo/photo4.jpg',
    after: '/demo/photo4.jpg',
    sliderPosition: 50
  }
])

const textToImageCases = ref<TextToImageCase[]>([
  {
    prompt: "一个小女孩在雨中撑着伞，背景是乡村风景",
    image: "/demo/image.png"
  },
  {
    prompt: "一只猫坐在窗台上看着夕阳",
    image: "/demo/image_after.png"
  },
  {
    prompt: "森林中的小木屋，炊烟袅袅",
    image: "/demo/bike.png"
  },
  {
    prompt: "海边的灯塔，风暴即将来临",
    image: "/demo/bike_after.png"
  },
  {
    prompt: "一个男孩和他的狗在草地上奔跑",
    image: "/demo/photo1.jpg"
  },
  {
    prompt: "繁星点点的夜空下的城市",
    image: "/demo/photo2.jpg"
  },
  {
    prompt: "樱花树下的小路",
    image: "/demo/樱花.png"
  },
  {
    prompt: "夏日海边的小镇",
    image: "/demo/photo3.jpg"
  },
  {
    prompt: "雪山下的温泉旅馆",
    image: "/demo/photo4.jpg"
  },
  {
    prompt: "小型文生图案例1",
    image: "/demo/text2img1.jpg"
  },
  {
    prompt: "小型文生图案例2",
    image: "/demo/text2img2.jpg"
  },
  {
    prompt: "小型文生图案例3",
    image: "/demo/text2img3.jpg"
  },
  {
    prompt: "小型文生图案例4",
    image: "/demo/text2img4.jpg"
  },
  {
    prompt: "小型文生图案例5",
    image: "/demo/text2img5.jpg"
  },
  {
    prompt: "小型文生图案例6",
    image: "/demo/text2img6.jpg"
  }
])

let activeIndex = ref(-1)
let isSliding = false

const startSliding = (e: MouseEvent | TouchEvent, index: number) => {
  e.preventDefault()
  isSliding = true
  activeIndex.value = index
  
  // Handle the initial slide position immediately
  handleSlide(e)
  
  document.addEventListener('mousemove', handleSlide)
  document.addEventListener('touchmove', handleSlide)
  document.addEventListener('mouseup', stopSliding)
  document.addEventListener('touchend', stopSliding)
}

const handleSlide = (e: MouseEvent | TouchEvent) => {
  if (!isSliding || activeIndex.value === -1) return
  
  // Prevent default behavior to avoid text selection during sliding
  e.preventDefault()
  
  const clientX = e instanceof MouseEvent ? e.clientX : e.touches[0].clientX
  const wrapper = document.querySelectorAll('.comparison-wrapper')[activeIndex.value]
  const bounds = wrapper?.getBoundingClientRect()
  
  if (!bounds) return
  
  const position = Math.min(Math.max(((clientX - bounds.left) / bounds.width) * 100, 0), 100)
  cases.value[activeIndex.value].sliderPosition = position
}

const stopSliding = () => {
  isSliding = false
  activeIndex.value = -1
  document.removeEventListener('mousemove', handleSlide)
  document.removeEventListener('touchmove', handleSlide)
  document.removeEventListener('mouseup', stopSliding)
  document.removeEventListener('touchend', stopSliding)
}

onBeforeUnmount(() => {
  document.removeEventListener('mousemove', handleSlide)
  document.removeEventListener('touchmove', handleSlide)
  document.removeEventListener('mouseup', stopSliding)
  document.removeEventListener('touchend', stopSliding)
})
</script>

<style scoped>
.cases-container {
  padding: 120px 0 80px;
  position: relative;
  z-index: 2; /* 确保内容在蒙板之上 */
  background-color: transparent; /* 改为透明背景，使用App.vue中的背景色 */
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
  color: #fff;
  height: auto;
  min-height: 100%;
}
.title {
  font-size: 2.5rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 8px;
}

.subtitle {
  font-size: 1.1rem;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 48px;
}

.section-title {
  font-size: 2.2rem;
  font-weight: 600;
  text-align: center;
  margin-top: 80px;
  margin-bottom: 8px;
}

.section-subtitle {
  font-size: 1.1rem;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 40px;
}

/* 图片对比部分样式 */
.comparison-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 40px;
  margin-top: 40px;
  contain: layout style; /* 提高渲染性能 */
}

.comparison-item {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  background-color: #1a1a1a;
  will-change: transform; /* 提示浏览器优化变换 */
  transform: translateZ(0); /* 启用GPU加速 */
}

.comparison-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  overflow: hidden;
  contain: strict; /* 严格的包含，提高性能 */
}

.image-base,
.image-top {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  will-change: transform; /* 提示浏览器优化变换 */
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  will-change: clip-path; /* 提示浏览器优化clip-path变化 */
}

.slider-vertical {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 40px;
  margin-left: -20px;
  cursor: ew-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  touch-action: none; /* Prevents browser handling of touch events */
}

.slider-line {
  position: absolute;
  width: 2px;
  height: 100%;
  background: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

.slider-button {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.slider-button::before,
.slider-button::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 2px;
  background: #666;
}

.slider-button::before {
  transform: translateX(-4px);
}

.slider-button::after {
  transform: translateX(4px);
}

.comparison-label {
  display: flex;
  justify-content: space-between;
  padding: 12px 16px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

.masonry-container {
  columns: 3 250px;
  column-gap: 20px;
  margin-top: 30px;
  contain: layout style; /* 提高渲染性能 */
}

.masonry-item {
  break-inside: avoid;
  margin-bottom: 20px;
  will-change: transform; /* 提示浏览器优化变换 */
  transform: translateZ(0); /* 启用GPU加速 */
}

.masonry-content {
  border-radius: 8px;
  overflow: hidden;
  background-color: #1a1a1a;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s ease;
  contain: content; /* 创建新的层叠上下文，提高渲染性能 */
}

.masonry-content:hover {
  transform: translateY(-5px) translateZ(0);
}

.masonry-image {
  width: 100%;
  display: block;
  object-fit: cover;
  will-change: transform; /* 提示浏览器优化变换 */
}

.masonry-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.9), rgba(0, 0, 0, 0.5), transparent);
  padding: 40px 20px 20px;
  text-align: center;
  transition: opacity 0.3s ease;
}

.prompt-text {
  font-size: 1rem;
  font-weight: 500;
  color: #fff;
  margin: 0;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  line-height: 1.4;
}

@media (max-width: 768px) {
  .masonry-content {
    transition: none; /* 移动设备上禁用过渡效果 */
  }
  
  .masonry-content:hover {
    transform: none !important; /* 移动设备上禁用悬停效果 */
  }
}
</style>
